<?php
/*
Plugin Name: De-Cat
Plugin URI: http://fholzhauer.de/b/
Description: Hide a category in the feeds
Version: 0.1 Beta
License: WTFPL
Author: Florian Holzhauer
Author URI: http://fholzhauer.de
*/

 /* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */

$decat_default = array(
	'hidecat'  	=> 0,
	'version'       => '0.1',
);

function fh_decat_admin() {
	global $decat_default;
	$decat_settings = get_option('fh_decat_settings');
	$decat_version = '0.1';
	$error = array();
	
	if(isset($_POST['submitted'])) {
		$new_hidecat = trim($_POST['hidecat']);
		if(eregi("^[0-9]*$",$new_hidecat)) {
		    $submitted_settings = array(
			    'hidecat'		=> $new_hidecat,
			    'version' 		=> $decat_version,
		    );

		    if($decat_settings != $submitted_settings) {
			    //something has changed. :)
			    update_option('fh_decat_settings', $submitted_settings);
			    $decat_settings = $submitted_settings;
		    }
		}
	} else if (empty($decat_settings)) {
                update_option('fh_decat_settings', $decat_default);
		$decat_settings = $decat_default;
	} ?>

	<div class="wrap">
		<h2>Hide Category from Feeds</h2>
		<form name="settings" action="<?php echo htmlentities($_SERVER[PHP_SELF]);?>?page=decat.php" method="post">
		<table width="100%" cellspacing="2" cellpadding="2" class="editform">
		<tr valign="top">
			<th scope="row" width="33%"><label for="hidecat">Hide Cat:</label></th>
			<td><select name="hidecat">
			<?php 
				$cats=get_all_category_ids();
				foreach($cats as $catid) {
					$catname=htmlentities(get_catname($catid));
					echo "<option ";
					if($catid == $decat_settings['hidecat']) echo "selected ";
					echo "value='$catid'>$catname</option>\n";
				}
			?>
			</select>
			<br/>Hide Category from RSS/Atom Feeds</td>
		</tr>
		</table>
		<p class="submit"><input type="hidden" name="submitted" /><input type="submit" name="Submit" value="Update Settings &raquo;" /></p>
		</form>
	</div>

<?php
	}
 	function do_decat_adminmenu() {
	   add_submenu_page('plugins.php', 'HideCat', 'HideCat', 9, basename(__FILE__), 'fh_decat_admin');
	}

	function do_decat($the_posts='') {
		if(!is_array($the_posts)) 
			return $the_posts;
		if(!is_feed())
			return $the_posts;

                $decat_settings = get_option('fh_decat_settings');

                if(empty($decat_settings)) {
			return $the_posts;
		} else {
                        $hide = $decat_settings['hidecat'];
			$retval = array();
			foreach($the_posts as $keyid => $post) {
				$objcats = get_the_category($post->ID);
				$addme = true;
				foreach($objcats as $obj) {
					if($hide == $obj->cat_ID) {
						$addme = false;
					}	
				}
				if($addme) $retval[]=$post;
			}
		}
		return $retval;
	}

    add_action('the_posts', 'do_decat');
    add_action('admin_menu',   'do_decat_adminmenu');


?>
